package com.bwie.xujiahaonewstitle.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bwie.newstitlexujiahao.R;
import com.bwie.xujiahaonewstitle.activity.AddTitleActivity;
import com.bwie.xujiahaonewstitle.activity.SearchActivity;
import com.bwie.xujiahaonewstitle.adapter.MyFragmentstatePageAdapter;
import com.bwie.xujiahaonewstitle.bean.HomeTitle;
import com.bwie.xujiahaonewstitle.httputils.HttpGetData;
import com.bwie.xujiahaonewstitle.httputils.Utils;
import com.bwie.xujiahaonewstitle.httputils.VolleyInterface;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称
 * 类描述：
 * 创建人：${徐家浩}
 * 创建时间：2017/2/17 19:32
 * 修改人：${徐家浩}
 * 修改备注：
 */
public class HomeFragment extends Fragment implements Serializable{
    private static final String TAG = "Fragment1";
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> list = new ArrayList<>();
    private View view;
    private String mString;
    private HomeTitle mHomeTitle;
    private Gson mGson;
    public static List<HomeTitle.DataBeanX.DataBean> mData;
    public ArrayList<String> mBaselist;
    private List<String> mBaselist2;
    private EditText mEditText;
    private TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.homefragment,null);
        mGson = new Gson();
        initView();
        return view;
    }
    private void initOnClick() {
        //搜索框
        mEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_form_right, R.anim.in_form_left);
            }
        });
        //添加标题
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), AddTitleActivity.class);
                String key = "key";
                intent.putExtra(key,(Serializable) (mBaselist));
                Log.d("传递数据",mBaselist.toString());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
        initOnClick();
    }
    private void initViews(){
        mViewPager = (ViewPager) view.findViewById(R.id.homefragment_viewpager);
        MyFragmentstatePageAdapter myFragmentstatePageAdapter = new MyFragmentstatePageAdapter(getActivity().getSupportFragmentManager(), list, mBaselist2, mBaselist);
        //最多可以预加载3个缓存
        mViewPager.setOffscreenPageLimit(0);
        mViewPager.setAdapter(myFragmentstatePageAdapter);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setupWithViewPager(mViewPager);
    }
    //初始化数据
    public void getData() {
        HttpGetData.RequestGet(getActivity(), Utils.THE_TITLE_INTERFACE, "123", new VolleyInterface(getActivity(), VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                Log.i(TAG, "onMySuccess: 哈哈哈哈" + result);
                mString = result;
                for (int i = 0; i < 15; i++) {
                    Fragment1_frag fragment1_frag = new Fragment1_frag();
                    list.add(fragment1_frag);
                }
                Log.i(TAG, "标题接口" + result);
                mHomeTitle = mGson.fromJson(result, HomeTitle.class);
                mData = mHomeTitle.getData().getData();
                Log.i(TAG, "Bean请求数据" + mData.toString());
                mBaselist = new ArrayList<>();
                mBaselist2 = new ArrayList<>();
                mBaselist.add(0, "推荐");
                mBaselist2.add("tuijian");
                for (int i = 0; i < 15; i++) {
                    mBaselist.add(mData.get(i).getName());
                    mBaselist2.add(mData.get(i).getCategory());
                }
                Log.i(TAG, "getData: " + mData.size());
                initViews();
            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });
    }
    private void initView() {
        mTabLayout = (TabLayout) view.findViewById(R.id.homefragment_tab);
        mEditText = (EditText) view.findViewById(R.id.search_EditText);
        mTextView = (TextView) view.findViewById(R.id.addTitle);
    }

}
